package at.codebase.web.servlets.admin;

import static at.codebase.web.util.i18n.I18NFactory.i18n;

import java.util.ArrayList;

import at.codebase.core.PageLinkFactory;
import at.codebase.core.ServletList;
import at.codebase.core.log.Log;
import at.codebase.db.entities.ELogMessage;
import at.codebase.web.templates.MainTemplate;
import at.codebase.web.util.LocalRequest.RequestInformation;
import at.codebase.web.util.URLParameters.ParameterDoesNotExistException;
import at.codebase.web.util.URLParameters.ParameterParsingException;
import at.codebase.web.util.i10n.L10N;
import at.codebase.web.util.servlet.SecureServlet;
import at.codebase.web.util.template.Template;
import at.codebase.web.util.template.TemplateEngine;

public class Overview extends SecureServlet {

	private final int LogMessagesPerPage = 20;
	
	@Override
	protected void initSecurityLevels(ArrayList<UserLevel> secLevels) {
		secLevels.add(UserLevel.ADMIN);
	}

	@Override
	protected void processRequestSecurityLevel(RequestInformation requestInfo, UserLevel level, RequestType requestType) {
		
		MainTemplate mainTemplate = new MainTemplate(requestInfo, writer());
		mainTemplate.setTitle("Admin Overview");
		
		Template overviewTemplate = TemplateEngine.getTemplate("admin/site_overview.html");
		overviewTemplate.setTemplate("adminMenuBar", TemplateEngine.getTemplate("admin/admin_menu_bar.html"));
		
		long userId = requestInfo.getUser().getId().longValue();
		
		int page;
		try {
			page = requestInfo.getParameters().getInteger("page");
			if (page < 1) {
				page = 1;
			}
		} catch (ParameterDoesNotExistException e) {
			page = 1;
		} catch (ParameterParsingException e) {
			page = 1;
		}
		int lastPage = (int) Math
				.ceil((double) Log.getLogMessageCountByType(3, userId)
						/ (double) LogMessagesPerPage);
		if (lastPage == 0) {
			lastPage = 1;
		}
		if (page > lastPage) {
			page = lastPage;
		}

		ArrayList<Template> rows = new ArrayList<Template>();
		for (ELogMessage logEntry : Log.getLogMessages(3, userId, (page - 1)
				* LogMessagesPerPage, LogMessagesPerPage)) {
			Template logEntryTemplate = TemplateEngine
					.getTemplate("log/log_entry.html");
			logEntryTemplate.setText("logType",
					String.valueOf(logEntry.getType()));
			logEntryTemplate.setText("logDate", String.valueOf(L10N.formatDate(
					requestInfo.getLocale(), logEntry.getTimestamp())));
			logEntryTemplate.setText("logTime", String.valueOf(L10N.formatTimeExact(
					requestInfo.getLocale(), logEntry.getTimestamp())));
			if(logEntry.getUser() != null){
				logEntryTemplate.setText("logUser", String.valueOf(logEntry.getUser().getId()));
			}else{
				logEntryTemplate.setText("logUser","null");
			}
			logEntryTemplate.setHTML("logMessage",
					Log.parseLogMessage(logEntry));
			rows.add(logEntryTemplate);
		}
		overviewTemplate.setText("date", i18n(requestInfo.getLocale())
				.string("date"));
		overviewTemplate.setText("time", i18n(requestInfo.getLocale())
				.string("time"));
		overviewTemplate.setText("message",
				i18n(requestInfo.getLocale()).string("message"));
		overviewTemplate.setTemplates("logEntries", rows);
		overviewTemplate.setHTML("pageLinks", PageLinkFactory.createLinks(
				ServletList.OverviewWithPage, page,
				lastPage));
		mainTemplate.setTemplate("body", overviewTemplate);
		mainTemplate.render(requestInfo.getLocale());
	}

}
